<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Request;
use think\Db;
use think\Cache;
use think\Log;
use fast\Http;

/**
 * 会员接口
 */
class Tian extends Api
{
    protected $noNeedLogin = ['get_qiwu_api', 'get_uid'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    //获取唯一的uid
    public function  get_uid(){
        $ret["uid"]=$this->getExtensionCode();
        $this->success(__('获取成功'), $ret);
   }
    /**
     * 苏格拉底接口
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function get_qiwu_api()
    {
        //header('Content-type:application/json');
        $appkey_find=Db::table("fa_sgld_setting")->where("key_name='appkey'")->find();
        $appkey=$appkey_find["key_value"];
        $appsecret_find=Db::table("fa_sgld_setting")->where("key_name='appsecret'")->find();
        $appsecret=$appsecret_find["key_value"];
        $url_find=Db::table("fa_sgld_setting")->where("key_name='qiwu_url'")->find();
        $url = $url_find["key_value"];

        $type= $this->request->post('type',0);// 0: 输入的题  2:输入回答的问题
        $msg= $this->request->post('msg');
        $uid= $this->request->post('uid');
        $post_data['appkey'] = $appkey;
        $post_data['timestamp'] =time();
        $post_data['uid'] = $uid;
        $post_data['verify'] = MD5($appsecret.$post_data['uid'].$post_data['timestamp']);
        //var_dump($post_data['verify'] );die;
        $post_data['msg'] =$msg;
        $post_data['sn'] ="aaa5";
        //$ret =Http::post($url, $post_data);
        //var_dump($post_data);
        $ret = json_decode(Http::post($url, $post_data), true);
        //var_dump($ret);die;
        if($ret["retcode"]==0){//0 获取成功
            $insert_data["uid"]=$uid;
            $insert_data["type"]=$type; // 0: 输入的题  2:输入回答的问题
            $insert_data["addtime"]=date("Y-m-d H:i:s",time());
            $insert_data["content"]=$msg;
            $insert_id=Db::table("fa_sgld_message")->insertGetId($insert_data);
            if($insert_id>0){//启悟返回值 记录
                if($type==0){//0: 输入的题
                    $insert_data["type"]=1;//1:输入题后的 启悟返回值
                    if(!empty($ret["data"]["dialogList"])){


                        $return_contents="";
                        foreach($ret["data"]["dialogList"] as $key=>$value){
                            if($value["npcName"]=="问了猫"){
                                if(empty($return_contents)){
                                    $return_contents=$value["text"];
                                }else{
                                    $return_contents=$return_contents."。".$value["text"];
                                }
                            }
                        }
                        $insert_data["content"]=$return_contents;
                    }else{
                        $insert_data["content"]=$ret["msg"];
                    }

                }elseif($type==2){// 2:输入回答的问题
                    $insert_data["type"]=3;// 3:输入回答问题后的 启悟返回值
                    if(!empty($ret["data"]["dialogList"])){
                        $return_contents="";
                        foreach($ret["data"]["dialogList"] as $key=>$value){
                            if($value["npcName"]=="问了猫"){
                                if(empty($return_contents)){
                                    $return_contents=$value["text"];
                                }else{
                                    $return_contents=$return_contents."。".$value["text"];
                                }
                            }
                        }
                        $insert_data["content"]=$return_contents;
                    }else{
                        $insert_data["content"]=$ret["msg"];
                    }
                    //$insert_data["content"]=$ret["msg"];
                }
                $insert_data["uid"]=$uid;
                $insert_data["pid"]=$insert_id;
                $insert_data["addtime"]=date("Y-m-d H:i:s",time());

                $insert_data["res"]=json_encode($ret,JSON_UNESCAPED_UNICODE);
                $insert_id=Db::table("fa_sgld_message")->insertGetId($insert_data);
            }
        }

        //对返回的数据字段处理后返回给前段
        if(!empty($ret["data"]["dialogList"])){
            $ret["data"]["dialogList_npcNames"]=array();
            foreach($ret["data"]["dialogList"] as $key=>$value){
                if($value["npcName"]=="问了猫"){
                    array_push($ret["data"]["dialogList_npcNames"],$value["text"]);
                }
            }
        }
        //var_dump($ret);
        $this->success(__('获取成功'), $ret);

    }



    //生成唯一编号
    public  function getExtensionCode()
    {
        $code = self::generate_password(5);
        $where["uid"]=$code;
        $others_is_have_bianma = Db::table("fa_sgld_message")->where($where)->find();
        if ($others_is_have_bianma) {
            //如果生成的邀请码存在，继续生成，直到不存在
            $code = self::getExtensionCode();
        }
        return $code;
    }
    public static function generate_password($length = 5)
    {
        $chars = '123456789';
        $password = "";
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }


}
